#ifndef ROCKET_NET_RPC_RPC_DISPATCHER_H
#define ROCKET_NET_RPC_RPC_DISPATCHER_H

/*
    1. Register a service object;
    2. Get func name;
    3. Get the request type and response type;
    4. Parse the object in request type from pb_data, make an empty object in
   response type;
    5. func(request, response);
    6. Serialize into a pb_data, package into a TinyPBProtocol;
*/

#include <google/protobuf/service.h>
#include <map>
#include <memory>

#include "../coder/tinypb_protocol.h"
#include "../tcp/tcp_connection.h"

namespace rocket {

class RpcDispatcher {

  public:
    static RpcDispatcher *GetRpcDispatcher();

  public:
    typedef std::shared_ptr<google::protobuf::Service> service_s_ptr;

    void dispatch(AbstractProtocol::s_ptr request,
                  AbstractProtocol::s_ptr response, TcpConnection *connection);

    void registerService(service_s_ptr service);

    void setTinyPBError(std::shared_ptr<TinyPBProtocol> msg, int32_t err_code,
                        const std::string err_info);

  private:
    bool parseServiceFullName(const std::string &full_name,
                              std::string &service_name,
                              std::string &method_name);

  private:
    std::map<std::string, service_s_ptr> m_service_map;
};

} // namespace rocket

#endif
